var express = require('express');
var router = express.Router();
var { housesModel } = require('../model/model')

/* GET home page. */
// 添加

router.get('/getall', async function (req, res, next) {
  let params = req.query
  let page = params.page
  let cate = params.cate
  let query = {}
  if (cate) {
    query.cate = cate
  }

  let pageSize = params.pageSize
  let house = await housesModel.find(query).skip((page - 1) * pageSize).limit(pageSize)
  // console.log(params, 'params');

  res.send({
    code: 200,
    house
  })
})

router.post('/paixu', async function (req, res, next) {
  let { flag } = req.body
  // console.log(flag, 'params');
  // 根据flag进行排序 true为升序 false为降序
  if (flag) {
    house = await housesModel.find().sort({ price: 1 })
  } else {
    house = await housesModel.find().sort({ price: -1 })
  }
  res.send({
    code: 200,
    house
  })
})

// 搜索
router.get('/getall1', async function (req, res, next) {
  // 
  let params = req.query
  // 模糊查询
  let name = params.name
  let newname = new RegExp(name, 'i')
  console.log(params, 'params');

  console.log(name, 'name');
  let query = {}
  if (name) {
    query.name = newname
  }
  let house = await housesModel.find({ name: newname })
  console.log(house, 'house');
  res.send({
    code: 200,
    house
  })
})
// 详情
router.post('/getgood', async (req, res, next) => {
  let { id } = req.body
  console.log(id, '-----------------id');
  let good = await housesModel.find({ _id: id })
  let goodlist = await housesModel.find()

  res.send({
    code: 200,
    good,
    goodlist
  })
})





































// router.get('/getall',async function(req, res, next) {
//   let shop= await shopsModel.find()
//   res.send({
//     code:200,
//     shop
//   })
// })

// router.get('/getinfo',async function(req, res, next) {
//   let id=req.query.id
//   // console.log(id);
//   let shopinfo= await shopsModel.find({_id:id})
//   res.send({
//     code:200,
//     shopinfo
//   })
// })
// router.post('/gocar',async function(req, res, next) {
//   let {params}=req.body
//   console.log(params,'params');

//   let num=params.num
//  await shopsModel.updateOne({_id:params.id},{buy:num})
//  let carlist=await shopsModel.find({_id:params.id})
//  console.log(carlist,'carlist');
//   res.send({
//     code:200,
//     carlist
//   })
// })

module.exports = router;
